// pages/win/win.js
var app = getApp();
var config = app.globalData.config;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShowTips: true,
    pageIndex: 1,
    pageSize: 50,
    activityId: 0,
    luckyUser: [],
    joinCount: 0,
    exchange: 'Online',
    isPushUser: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var {
      activityId,
      joinCount,
      exchange,
      isPushUser
    } = options;
    if (!!activityId) {
      this.setData({
        activityId: activityId,
        joinCount: joinCount,
        exchange: exchange,
        isPushUser: isPushUser==1
      })
      this.getActivityUser()
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (this.data.isShowTips) {
      return;
    }
    var page = this.data.pageIndex + 1;
    this.setData({
      pageIndex: page
    })
    this.getActivityUser();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  getActivityUser() {
    var that = this;
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    wx.request({
      url: config.luckyUserUrl,
      method: "GET",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        id: that.data.activityId,
        pageIndex: that.data.pageIndex,
        pageSize: that.data.pageSize
      },
      success: function(res) {
        // 回调函数
        var moment_list = that.data.luckyUser;
        for (var i = 0; i < res.data.data.reList.length; i++) {
          var exist_detail = moment_list.find(x => x.detail.id == res.data.data.reList[i].detail.id);
          if (!exist_detail) {
            moment_list.push(res.data.data.reList[i]);
          } else {
            moment_list[i].users.concat(res.data.data.reList[i].users)
          }
        }
        // 设置数据
        that.setData({
          luckyUser: moment_list,
          count: res.data.data.count,
          isShowTips: res.data.data.reList.length == 0 || that.data.pageSize > res.data.data.count
        })
      },
      fail(res) {},
      complete(res) {
        wx.hideLoading();
      }
    })
  },
  onAddress(e) {
    var address = e.currentTarget.dataset.text;
    if (!!address) {
      wx.setClipboardData({
        data: address,
        success() {
          wx.showToast({
            title: '复制成功',
            icon: 'none'
          })
        }
      })
    }
  }
})